<template>
    <div>
        <div>
            创建时间：
            <el-date-picker
                    v-model="searchTime"
                    type="daterange"
                    align="right"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    :picker-options="pickerOptions">
            </el-date-picker>
            <el-button class="ml30" type="primary" @click="searchDataByTime" id="monitorEnter">查询</el-button>
            <el-button type="primary" @click="newPage('new', false)">新建</el-button>
        </div>
        <el-table
                :data="tableData"
                highlight-current-row
                style="width: 100%;margin-top: 20px;">
            <el-table-column
                    type="index"
                    label="序号"
                    align="center"
                    width="50">
            </el-table-column>
            <el-table-column
                    property="version"
                    align="center"
                    label="版本号">
            </el-table-column>
            <el-table-column
                    property="extractBaseNum"
                    align="center"
                    label="计提基数">
                <template slot-scope="scope">
                    {{extractBaseNum[scope.row.extractBaseNum]}}
                </template>
            </el-table-column>
            <el-table-column
                    property="id"
                    align="center"
                    label="计提比例">
                <template slot-scope="scope">
                    <span v-if="scope.row.extractType ===2">{{scope.row.extractProportion}}%</span>
                    <a v-else href="javascript:;;" @click="viewStageExtract(scope.row.id)">查看</a>
                </template>
            </el-table-column>
            <el-table-column
                    property="createTime"
                    align="center"
                    label="创建时间">
            </el-table-column>
            <el-table-column
                    property="creatorName"
                    align="center"
                    label="创建人">
            </el-table-column>
            <el-table-column
                    property="address"
                    align="center"
                    width="100"
                    label="详细信息">
                <template slot-scope="scope">
                    <a href="javascript:;;" @click="newPage('view', scope.row.id)">查看</a>
                    <a href="javascript:;;" @click="newPage('edit', scope.row.id)">编辑</a>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagebar">
            <el-pagination
                    background
                    layout="total, sizes, prev, pager, next, jumper"
                    :current-page.sync="tableConfig.pageNum"
                    :page-sizes="[10, 20, 30, 40]"
                    :page-size="tableConfig.pageSize"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :total="tableConfig.totalSize">
            </el-pagination>
        </div>

        <!-- 计提比例弹框 -->
        <el-dialog
                title="查看计提比例"
                :visible.sync="scaleDialogVisible"
                width="500px">
            <div class="profits">
                <el-table
                        :data="settleRangeList"
                        border
                        highlight-current-row
                        style="width: 100%;margin-top: 20px; max-height: 450px; overflow: auto;">
                    <el-table-column
                            align="center"
                            label="利润区间(万元)">
                        <template slot-scope="scope">
                            <span v-if="scope.row.lowerMark === 1">
                                {{scope.row.upper}}以下
                            </span>
                            <span v-else-if="scope.row.upperMark ===1">
                                {{scope.row.lower}}以上
                            </span>
                            <span v-else>
                                {{scope.row.lower}} - {{scope.row.upper}}
                            </span>
                        </template>
                    </el-table-column>
                    <el-table-column
                            property="extractProportion"
                            align="center"
                            label="计提比例">
                        <template slot-scope="scope">
                            {{scope.row.extractProportion}}%
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <span slot="footer">
                <el-button @click="scaleDialogVisible = false">关闭</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
    import api from '@/api/apiPath'
    import {extractCategory, ruleEffect, calNode, extractBaseNum} from '../fieldMap'

    export default {
        name: "profitSettlement",
        title:'利润结算标准',
        data() {
            return {
                searchTime: "",
                settleRangeList: [],
                pickerOptions: {
                    shortcuts: [{
                        text: '最近一周',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近一个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                            picker.$emit('pick', [start, end]);
                        }
                    }, {
                        text: '最近三个月',
                        onClick(picker) {
                            const end = new Date();
                            const start = new Date();
                            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                            picker.$emit('pick', [start, end]);
                        }
                    }]
                },
                scaleDialogVisible: false,
                tableData: [],
                tableConfig: {
                    startTime: "",
                    endTime: "",
                    pageNum: 1,
                    pageSize: 10,
                    totalSize: 10
                },

                extractCategory,
                ruleEffect,
                calNode,
                extractBaseNum,
            }
        },
        created() {
            this.getSettleListByPage()
        },
        mounted() {

        },
        activated(){
            this.searchDataByTime()
        },
        methods: {
            searchDataByTime() {
                if (this.searchTime === null) {
                    this.searchTime = ["", ""]
                }
                if (this.searchTime.length) {
                    this.tableConfig.startTime = +new Date(this.searchTime[0])
                    this.tableConfig.endTime = +new Date(this.searchTime[1])
                }
                this.tableConfig.pageNum = 1;
                this.getSettleListByPage();
            },
            newPage(type, id) {
                this.$router.push({path: "./profitSettlementHandle", query: {type: type, id: id}})
            },
            getSettleListByPage() {
                this.axios.post(api.getSettleListByPage, {
                    startTime: this.tableConfig.startTime,
                    endTime: this.tableConfig.endTime,
                    pageNum: this.tableConfig.pageNum,
                    pageSize: this.tableConfig.pageSize
                }).then((res) => {
                    console.log(res);
                    this.tableData = res.data.data.list;
                    this.tableConfig.totalSize = res.data.data.total;
                })
            },
            viewStageExtract(id) {
                this.scaleDialogVisible = true;
                this.axios.post(api.viewStageExtract, {
                    settleId: id
                }).then((res) => {
                    this.settleRangeList = res.data.data.settleRangeList
                })
            },
            handleSizeChange(size) {
                this.tableConfig.pageSize = size
                this.getSettleListByPage()
            },
            handleCurrentChange(page) {
                this.tableConfig.pageNum = page
                this.getSettleListByPage()
            },
        },
        components: {},
        computed: {}
    }
</script>

<style scoped>
    .profits .el-table::before{
        height: 0;
    }
</style>
